package ext.generic.part;

import com.ptc.core.components.forms.*;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.misc.NmContext;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.util.beans.NmClipboardItem;
import com.ptc.netmarkets.util.beans.NmClipboardUtility;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import java.util.*;

import org.apache.log4j.Logger;

import wt.fc.WTObject;
import wt.fc.WTReference;
import wt.fc.collections.WTArrayList;
import wt.fc.collections.WTCollection;

import wt.log4j.LogR;

import wt.part.WTPart;
import wt.part.WTProductInstance2;

import wt.session.SessionHelper;
import wt.session.SessionServerHelper;

import wt.util.*;

import wt.workflow.work.*;

import wt.part.*;

import wt.fc.*;

/**
 * 零部件签审流程中"添加随签对象"的action响应方法类
 */
public class AddReviewObjsFormProcessor extends DefaultObjectFormProcessor
{

	//public static WTArrayList arry=new WTArrayList();
	
    public AddReviewObjsFormProcessor()
    {
        redirectURL = null;
    }
    /**
     *主要处理方法
     * @param nmcommandbean
     * @param list
     * @return
     * @throws WTException
     */
    public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException
    {
        //Vector reviewObjs = new Vector();
    	List reviewObjs = new ArrayList();
        java.util.Locale locale = SessionHelper.getLocale();
        FormResult formresult = new FormResult();
        formresult.setStatus(FormProcessingStatus.SUCCESS);
        wt.fc.Persistable persistable = nmcommandbean.getPrimaryOid().getWtRef().getObject();
        if (persistable instanceof WorkItem)
        {
            WorkItem workItem = (WorkItem) persistable;
            WfAssignedActivity wfAssignedActivity = (WfAssignedActivity) workItem.getSource().getObject();
            reviewObjs = (List) (wfAssignedActivity.getContext().getValue("reviewObjs"));
           
           // arry=(WTArrayList)reviewObjs;
            doAddOperation(nmcommandbean, reviewObjs);
            
            for(int j=0;j<reviewObjs.size();j++){
            	System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>reviewObjs="+reviewObjs.get(j));
            }
            wfAssignedActivity.getContext().setValue("reviewObjs", reviewObjs);
            PersistenceHelper.manager.save(wfAssignedActivity);
            reviewObjs = (List) (wfAssignedActivity.getContext().getValue("reviewObjs"));
        }       
       // super.doOperation(nmcommandbean, list);
       
        return formresult;
    }

    protected FormResult setRefreshInfo(FormResult formresult, NmCommandBean nmcommandbean, List list) throws WTException
    {
        DynamicRefreshInfo dynamicrefreshinfo = new DynamicRefreshInfo();
        dynamicrefreshinfo.setLocation(nmcommandbean.getActionOid());
        dynamicrefreshinfo.setOid(nmcommandbean.getActionOid());
        NmCommandBean _tmp = nmcommandbean;
        dynamicrefreshinfo.setAction("U");
        formresult.addDynamicRefreshInfo(dynamicrefreshinfo);
        return formresult;
    }
    /**
     *添加随签对象的子方法
     * @param nmcommandbean
     * @param reviewObjs
     * @throws WTException
     */
    public static void doAddOperation(NmCommandBean nmcommandbean, List reviewObjs) throws WTException
    {
    	 ArrayList list = nmcommandbean.getSelected();
        if (VERBOSE)
            System.out.println("====doAddOperation=====   " + list.size() + " objs ared selected!");
        for (int i = 0; i < list.size(); i++)
        {
        	 NmContext nmcontext = (NmContext) list.get(i);
             Persistable persistable = nmcontext.getTargetOid().getWtRef().getObject();
            if (VERBOSE)
                System.out.println("doAddOperation   Process " + persistable.getIdentity() + " obj!");
            boolean flag = reviewObjs.add(persistable);
            if (VERBOSE && flag)
                System.out.println("doAddOperation   " + persistable.getIdentity() + " is added to reviewObjs!");
        }
        SessionServerHelper.manager.setAccessEnforced(true);
        
        
    }

    private static final String RESOURCE = "com.ptc.netmarkets.util.utilResource";
    private static final Logger log;
    protected String redirectURL;
    private static boolean VERBOSE = false;

    static
    {
        try
        {
            log = LogR.getLogger(ext.generic.part.AddReviewObjsFormProcessor.class.getName());
            //VERBOSE= (WTProperties.getLocalProperties()).getProperty("ext.generic.part.verbose", false);
            VERBOSE = true;
        }
        catch (Exception exception)
        {
            throw new ExceptionInInitializerError(exception);
        }
    }
}
